package com.ziyia.edittext;

import androidx.appcompat.app.AppCompatActivity;


import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {
    private EditText editText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editText = findViewById(R.id.et_one);

//         文本改变事件
        editText.addTextChangedListener(new TextWatcher() {
            private String mBefore;
            private int mCursor;

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                mBefore = s.toString();
                mCursor = start;
                Log.d("修改之前的内容", s.toString());
                Log.d("修改之前内容的长度", Integer.valueOf(start).toString());
                Log.d("字符串中将被修改字符串的长度  ", Integer.valueOf(count).toString());
                Log.d("新增内容的长度", Integer.valueOf(after).toString());
//                editText.setText(s.toString());
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
//                Log.d("s", s.toString());
//                Log.d("start", Integer.valueOf(start).toString());
//                Log.d("before", Integer.valueOf(before).toString());
//                Log.d("count", Integer.valueOf(count).toString());
            }

            @Override
            public void afterTextChanged(Editable s) {
                Log.d("text_after = ", editText.getText().toString());
//                if (s.toString().contains(" ")) {
//                    editText.removeTextChangedListener(this);
//                    editText.setText(mBefore);
//                    editText.addTextChangedListener(this);
//                    editText.setSelection(mCursor);
//                }
            }
        });
    }
}